home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Graphics / source / plane_3d.cp < prev    next >
Encoding:
Text File  |  1995-03-25  |  6.2 KB  |  112 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    plane_3d.cp
  3. //    Date:                    8/26/94
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains the methods for a plane_3d
  7. //
  8. //------------------------------------------------------------------------------
  9.  
  10. #include "utility.h"
  11. #include "plane_3d.h"
  12.  
  13. //------------------------------------------------------------------------------
  14. //    constructor
  15. //------------------------------------------------------------------------------
  16. plane_3d::plane_3d (const vector_3d &v, const point_3d &p)                                            //    normal constructor
  17. : tuple_3d (v[X], v[Y], v[Z], - (v | p))                                                                                //    superclass constructor
  18. {                                                                                                                                                                //    begin
  19. }                                                                                                                                                                //    end
  20.  
  21. //------------------------------------------------------------------------------
  22. //    constructor
  23. //------------------------------------------------------------------------------
  24. plane_3d::plane_3d (const point_3d &a, const point_3d &b, const point_3d &c)        //    normal constructor
  25. {                                                                                                                                                                //    begin
  26.     vector_3d    v = ((b - a) ^ (c - b)).Normalize ();                                                                //    compute the normal vector_3d
  27.     tuple_3d::operator () (v[X], v[Y], v[Z], - (v | a));                                                    //    set up the plane_3d values
  28. }                                                                                                                                                                //    end
  29.  
  30. //------------------------------------------------------------------------------
  31. //    constructor
  32. //------------------------------------------------------------------------------
  33. plane_3d::plane_3d (real a, real b, real c, real d) : tuple_3d (a, b, c, d)            //    component constructor
  34. {                                                                                                                                                                //    begin
  35. }                                                                                                                                                                //    end
  36.  
  37. //------------------------------------------------------------------------------
  38. //    constructor
  39. //------------------------------------------------------------------------------
  40. plane_3d::plane_3d (const plane_3d &p) : tuple_3d (p)                                                        //    copy constructor
  41. {                                                                                                                                                                //    begin
  42. }                                                                                                                                                                //    end
  43.  
  44. //------------------------------------------------------------------------------
  45. //    constructor
  46. //------------------------------------------------------------------------------
  47. plane_3d::plane_3d (const tuple_3d &t) : tuple_3d (t)                                                        //    constructor from a tuple_3d
  48. {                                                                                                                                                                //    begin
  49. }                                                                                                                                                                //    end
  50.  
  51. //------------------------------------------------------------------------------
  52. //    destructor
  53. //------------------------------------------------------------------------------
  54. plane_3d::~plane_3d (void)                                                                                                            //    destructor
  55. {                                                                                                                                                                //    begin
  56. }                                                                                                                                                                //    end
  57.  
  58. //------------------------------------------------------------------------------
  59. //    assignment
  60. //------------------------------------------------------------------------------
  61. plane_3d    &plane_3d::operator = (const plane_3d &p)                                                            //    assignment operator
  62. {                                                                                                                                                                //    begin
  63.     tuple_3d::operator () (p[X], p[Y], p[Z], p[W]);                                                                //    copy the plane_3d values
  64.     return *this;                                                                                                                                    //    return the reference to this
  65. }                                                                                                                                                                //    end
  66.  
  67. //------------------------------------------------------------------------------
  68. //    assignment
  69. //------------------------------------------------------------------------------
  70. plane_3d    &plane_3d::operator = (const tuple_3d &t)                                                            //    assignment operator
  71. {                                                                                                                                                                //    begin
  72.     tuple_3d::operator = (t);                                                                                                            //    copy the plane_3d values
  73.     return *this;                                                                                                                                    //    return the reference to this
  74. }                                                                                                                                                                //    end
  75.  
  76. //------------------------------------------------------------------------------
  77. //    compute the plane_3d equation
  78. //------------------------------------------------------------------------------
  79. void    plane_3d::Define (const vector_3d &v, const point_3d &p)                                    //    compute the plane_3d equation
  80. {                                                                                                                                                                //    begin
  81.     tuple_3d::operator () (v[X], v[Y], v[Z], - (v | p));                                                    //    set the tuple_3d values
  82. }                                                                                                                                                                //    end
  83.  
  84. //------------------------------------------------------------------------------
  85. //    compute the plane_3d equation
  86. //------------------------------------------------------------------------------
  87. void    plane_3d::Define (const point_3d &a, const point_3d &b, const point_3d &c)//    compute the plane_3d equation
  88. {                                                                                                                                                                //    begin
  89.     vector_3d    v = (b - a) ^ (c - b);                                                                                            //    compute the normal vector_3d
  90.     tuple_3d::operator () (v[X], v[Y], v[Z], - (v | a));                                                    //    set up the plane_3d values
  91. }                                                                                                                                                                //    end
  92.  
  93. //------------------------------------------------------------------------------
  94. //    invert the plane_3d
  95. //------------------------------------------------------------------------------
  96. void    plane_3d::Invert (void)                                                                                                        //    invert the values of the plane_3d equation
  97. {                                                                                                                                                                //    begin
  98.     xyz[X] *= R(-1.0); xyz[Y] *= R(-1.0); xyz[Z] *= R(-1.0); xyz[W] *= R(-1.0);        //    reverse the tuple_3d values
  99. }                                                                                                                                                                //    end
  100.  
  101. //------------------------------------------------------------------------------
  102. //    compute the distance at which the ray intersects the plane_3d
  103. //------------------------------------------------------------------------------
  104. real        plane_3d::RayIntersection (const ray &r) const                                                    //    return the distance along the ray at which the intersection occurs
  105. {                                                                                                                                                                //    begin
  106.     real costheta = -(r.Direction () | *this);                                                                        //    compute the cosine of the angle between the ray and the plane_3d normal
  107.     if (abs (costheta) < EPSILON) return R(-1.0);                                                                    //    return a -1 for plane_3d and ray parallel
  108.     return (r.Origin () | *this) / costheta;                                                                            //    compute the distance from the ray origin to the plane_3d, and divide by the cosine 
  109. }                                                                                                                                                                //    end
  110.  
  111. //------------------------------------------------------------------------------
  112.